﻿var tab = null;
var tree = null;
var preTabId = "homeTab";
var addTab = function (tabid, text, url) {
    preTabId = tab.getSelectedTabItemID() || "homeTab";
    //alert(preTabId);
    tab.removeOther(preTabId);
    if (tab.isTabItemExist(tabid)) {
        tab.removeTabItem(tabid);
    }
    tab.addTabItem({ tabid: tabid, text: text, url: url });
};

$(function () {
    $.ajaxSetup({
        cache: false
    });


    //布局
    $("#layout").ligerLayout({
        leftWidth: 190,
        height: '100%',
        heightDiff: -26,
        allowTopResize: true,
        allowLeftCollapse: false,
        space: 4,
        onHeightChanged: function (options) {
            if (tab) {
                tab.addHeight(options.diff);
            }
        }
    });

    //Tab
    $("#framecenter").ligerTab({
        height: $(".l-layout-center").height()
    });
    tab = $("#framecenter").ligerGetTabManager();


    //tree
    $("#tree").ligerTree(
    {
        checkbox: false,
        nodeWidth: '100%',
        btnClickToToggleOnly: false,
        url: '/Home/Module',
        idFieldName: "ModuleID",
        parentIDFieldName: "parentCode",
        textFieldName: "ModuleName",
        attribute: ["ModuleID", "id"],
        onClick: function (node) //这里的异步加载逻辑取决于你的数据库设计，把选中节点的id传回去，传回子节点json然后加载
        {
            if (node.data && node.data.isLeaf) {
                addTab(node.data.id, node.data.ModuleName, node.data.ModulePath);
            }
        },
        single: true,
        // true 有子节点  false 无子节点
        isLeaf: function (treenodedata) {
            return !treenodedata.isLeaf;
        }
    });

    tree = $("#tree1").ligerGetTreeManager();
    $("#pageloading").hide();

    addTab(preTabId, "主页", "/Home/Welcome");

    $("#changePwd").click(function () {
        addTab('CJPWD', '修改密码', '/Home/ChangePassword');
    });
});